package org.springblade.modules.admin.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.tool.api.R;
import org.springblade.modules.admin.service.MaintainSignSetingService;
import org.springblade.modules.admin.vo.MaintainSignSetingVO;
import org.springblade.modules.admin.vo.SignSetingGroupUserVO;
import org.springblade.modules.admin.vo.SignUserVO;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 考勤配置
 *
 * @author lsay
 */
@RestController
@AllArgsConstructor
@RequestMapping("/sign/setting")
@Api(value = "考勤配置", tags = "考勤配置接口")
public class SignSettingController extends BladeController {

	private final MaintainSignSetingService signSetingService;

	/**
	 * 获取考勤配置列表
	 *
	 * @return
	 */
	@GetMapping("/detailList")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "获取考勤配置列表")
	public R<List<MaintainSignSetingVO>> detailList(@ApiParam(value = "考勤日历年份") @RequestParam(required = false) Integer year) {
		return R.data(signSetingService.detailList(year));
	}

	/**
	 * 新增/编辑考勤配置
	 *
	 * @param maintainSignSeting
	 * @return
	 */
	@PostMapping("/saveOrUpdate/signSeting")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "新增/编辑考勤配置")
	public R<Boolean> saveOrUpdateSignSeting(@RequestBody MaintainSignSetingVO maintainSignSeting) {
		if (maintainSignSeting.getUserIds() != null) {
			maintainSignSeting.setUserIds(maintainSignSeting.getUserIds().stream().distinct().collect(Collectors.toList()));
		}
		return R.data(signSetingService.saveOrUpdateSignSeting(maintainSignSeting));
	}

	@GetMapping("/getOtherSignSetingUsers")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "获取已参与考勤的用户")
	public R<List<Long>> getOtherSignSetingUsers(@ApiParam(value = "考勤配置id") @RequestParam(required = false) Long signSetingId) {
		return R.data(signSetingService.getOtherSignSetingUsers(signSetingId));
	}

	@GetMapping("/getHolidays")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "获取年度所有节假日")
	public R getHolidays(@ApiParam(value = "获取年度所有节假日，默认本年") @RequestParam(required = false) Integer year) {
		return R.data(signSetingService.getHolidays(year));
	}


	@GetMapping("/getHolidays/v2")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "获取年度所有节假日")
	public R getHolidaysV2(@ApiParam(value = "获取年度所有节假日，默认本年") @RequestParam(required = false) Integer year) {
		return R.data(signSetingService.getHolidaysV2(year));
	}

	@GetMapping("/getSignSetingUserInfos")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "获取考勤组的用户信息")
	public R<Map<Long, List<SignSetingGroupUserVO>>> getSignSetingUserInfos() {
		return R.data(signSetingService.getSignSetingUserInfos());
	}

}
